package com.gxw.rxjavatest.manager;

import android.util.Log;

import com.google.gson.Gson;
import com.gxw.rxjavatest.data.respone.GetInfoResponse;
import com.gxw.rxjavatest.model.bean.LoginID;

import org.json.JSONObject;
import org.xutils.http.app.ResponseParser;
import org.xutils.http.request.UriRequest;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by GXW on 2016/4/7 0007.
 * email:603004002@qq.com
 */
public class JsonResponseParser implements ResponseParser {

    private String TAG = JsonResponseParser.class.getName().toString();

    @Override
    public void checkResponse(UriRequest request) throws Throwable {

    }

    @Override
    public Object parse(Type resultType, Class<?> resultClass, String result) throws Throwable {


        Log.i(TAG, "resultType:" + resultType.toString());
        Log.i(TAG, "resultClass:" + resultClass.getName());
        Log.i(TAG, "result:" + result);

        if (resultClass == List.class) {

            Log.i(TAG," ---------------- 到这了吗 1---------------- ");

            List<GetInfoResponse> list = new ArrayList<>();
            JSONObject jsonObject = new JSONObject(result);
            String code = jsonObject.getString("code");
            String msg = jsonObject.getString("msg");
            String data = jsonObject.getString("data");

            Gson gson = new Gson();
            LoginID loginID = gson.fromJson(data, LoginID.class);
            GetInfoResponse getInfoResponse = new GetInfoResponse();
            getInfoResponse.setData(loginID);
            getInfoResponse.setCode(code);
            getInfoResponse.setMsg(msg);
            list.add(getInfoResponse);
            return list;
        } else {

            Log.i(TAG," ---------------- 到这了吗 2---------------- ");

        }

        return null;
    }
}
